package com.lsyw.intergrate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsyw.intergrate.common.Result;
import org.springframework.util.CollectionUtils;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import com.lsyw.intergrate.service.SysUsersRolesService;
import com.lsyw.intergrate.entity.SysUsersRoles;

import java.util.*;

import io.swagger.annotations.Api;
import lombok.Data;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 用户角色关联 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2023-04-21
 */
@Api(tags = "用户角色关联")
@RestController
@RequestMapping("/sys-users-roles")
public class SysUsersRolesController {


    @Autowired
    private SysUsersRolesService sysUsersRolesService;


    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    public Result getById(String id) {
        return Result.successResult(sysUsersRolesService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public Result save(@RequestBody SysUsersRoles sysUsersRoles) {
        List<String> roleIds = sysUsersRoles.getRoleIds();
        roleIds.stream().forEach(roleId -> {
            sysUsersRolesService.save(new SysUsersRoles().setRoleId(roleId).setUserId(sysUsersRoles.getUserId()));
        });
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id/{id}")
    public Result delete(@PathVariable(value = "id") String ids) {
        String[] idsStrs = ids.split(",");
        for (String id : idsStrs) {
            sysUsersRolesService.removeById(Long.parseLong(id));
        }
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result updateById(@RequestBody SysUsersRoles sysUsersRoles) {
        sysUsersRolesService.remove(new QueryWrapper<SysUsersRoles>().eq("user_id", sysUsersRoles.getUserId()));
//        sysUsersRolesService.updateById(sysUsersRoles);
        List<String> roleIds = sysUsersRoles.getRoleIds();
        roleIds.stream().forEach(roleId -> {
            sysUsersRolesService.save(new SysUsersRoles().setRoleId(roleId).setUserId(sysUsersRoles.getUserId()));
        });
        return Result.successResult(null);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    public Result list(@RequestBody SysUsersRolesReqVo sysUsersRoles) {
        final LambdaQueryWrapper<SysUsersRoles> lambda = new QueryWrapper<SysUsersRoles>().lambda();
        this.buildCondition(lambda, sysUsersRoles);
        return Result.successResult(sysUsersRolesService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<SysUsersRoles> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        final LambdaQueryWrapper<SysUsersRoles> lambda = new QueryWrapper<SysUsersRoles>().lambda();
        //TODO 自己的查询逻辑
        return Result.successResult(this.sysUsersRolesService.page(page, lambda));
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<SysUsersRoles> lambda, SysUsersRolesReqVo param) {
        if (!StringUtils.isEmpty(param.getUserId())) {
            lambda.eq(SysUsersRoles::getUserId, param.getUserId());
        }
        if (!StringUtils.isEmpty(param.getRoleId())) {
            lambda.eq(SysUsersRoles::getRoleId, param.getRoleId());
        }
        lambda.orderBy(true, false, SysUsersRoles::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class SysUsersRolesReqVo extends SysUsersRoles {
    }


}
